<?php
class AppController extends BaseController {
    //Текущая страница
    protected $page;
    protected $chain_url = '';
	protected $chain = array(array('url'=>'', 'text'=>'Главная'));

    protected $catalogue_tree;
	protected $catalogue_id_tree_map;

    function addChain($idn, $name, $direct_url = FALSE) {
    	if ($this->chain_url)
		    $this->chain_url .= '/';
			    	
    	$this->chain_url .= $idn;
		if ($direct_url)
		    $url = $direct_url;
		else
		    $url = $this->chain_url;
		  
		$this->chain[] = array('url'=>$url, 'text'=>$name);
    }
	
	function addTreeChain($chain) {
		$data = $this->catalogue_tree;
		foreach ($chain as $el) {
			$data = $data[$el];
			$this->addChain($data['idn'], $data['title']);
			$data = $data['nodes'];			
		}		
	}
	
	function getTreeElement() {
		$args = func_get_args();
		$data = $this->catalogue_tree;
		foreach ($args as $el) {
			$value = $data[$el];
			$data = $data[$el]['nodes'];
		}
		return $value;
	}
	
	function catalogueIdTreeMap($id) {
		return $this->catalogue_id_tree_map[$id];		
	}
	
	//Создание дерева каталогов
	function create_catalogue_tree($class_name) {
		$set_class = $class_name.'Set';
		$set = new $set_class();
				
        $nodes = $set->get();
        $data = array();
		$url = '';
		$this->catalogue_tree = array($nodes[1]->getField('idn')=>createTree($nodes[1], $nodes, $url, $this->catalogue_id_tree_map));
        
        /*$this->catalog_data = $data;
        
        $tree_data = array();
        generateTree($tree_data, $nodes[1], $nodes, array('idn', 'title'), 'id');
        $this->catalog_tree = array($tree_data['id']=>$tree_data);
        
        $res = '';
        $segments = array();
        $data = renderUrlTree($data, 0, $segments);
        $this->addBlockData('left_menu', 'menu', simpleRenderTree($data, 0, $res, 2));*/      
	}
	
	
	

    function __construct($params = NULL) {
        //Загружаем страницу из базы данных
		/*
        $controller_name = Local::get('controller');
        $method_name = Local::get('method');
        
        $alt_idn = $idn = $controller_name.'/'.$method_name;
        
        //Возмонжый альтернативные вариант имени
        if ($method_name == 'index')
            $alt_idn = 'main/'.$controller_name;
        
        //Ищем страницу
        $set = new PageSet(q::obj('page')->select()->where('idn', $idn)->or_where('idn', $alt_idn)->end);
        
        //Если ничего не нашли, значит это страница 404;
        if ($set->isEmpty()) {          
            $idn = '404';
            $set = new PageSet(q::obj('page')->select()->where('idn', '404')->end);
			
			//Отправляем правильный заголовок
			header("HTTP/1.1 404 Not Found");
        }
        
        $this->page = $set->getOne();
		
		$this->view_data['page'] = $this->page->getFields('title', 'content');
        $this->view_name = '_pages/'.$this->page->getField('idn');*/
		
		parent::__construct($params);
		$this->view_name = Path::page_default();
		$this->create_catalogue_tree('MyCatalogue');   
    }
	
	//Загрузка блока из базы данных
	function loadBlock($block_name) {
		$set = new BlockSet(q::obj('Block')->select()->where('idn', $block_name)->end);
		$this->addBlockData($block_name, $set->getOne()->getFields());	
	}
	
	function __destruct() {
		$this->addBlockData('chain', array('chain'=>$this->chain));
		if ($this->page) $this->addViewData('page', $this->page->getFields());
		parent::__destruct();
	}
	  
}
?>